#include "managementwidget.h"
#include "ui_managementwidget.h"
#include <QMessageBox>
#include <QCryptographicHash>

ManagementWidget::ManagementWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManagementWidget)
{
    ui->setupUi(this);
    ui->removeAccessRightButton->setEnabled(false);
}

ManagementWidget::~ManagementWidget()
{
    delete ui;
}

void ManagementWidget::setOwner(const QString &username)
{
    owner = username;
}

void ManagementWidget::userList(QStringList *list)
{
    foreach(const QString &username, *list) {
        ui->userList->addItem(username);
    }
    emit disconnectUserListSignal();
}

void ManagementWidget::userListError(const QString &errorMessage)
{
    QMessageBox::information(this, tr("Users list loading failed try logging in again"), errorMessage);
    emit disconnectUserListSignal();
}

void ManagementWidget::accessList(QStringList *list)
{
    ui->privilegeList->clear();
    foreach(const QString &username, *list) {
        ui->privilegeList->addItem(username);
    }
}

void ManagementWidget::accessListError(const QString &errorMessage)
{
     QMessageBox::information(this, tr("Users list loading failed try logging in again"), errorMessage);
}

void ManagementWidget::loadUserList(UserData *user) // non-slot, public function
{
    emit loadUsers(user);
}

void ManagementWidget::on_userList_itemClicked(QListWidgetItem *item)
{
    //ui->addAccessRightButton->setEnabled(false);
    ui->removeAccessRightButton->setEnabled(false);
    qDebug() << "management selected an item on userlist: " << item->text();
    if (item->text() != "")
    {
        /*
        ui->userList->setEnabled(false);
        ui->privilegeList->setEnabled(false);
        ui->addPrivilegeButton->setEnabled(false);
        ui->addUserButton->setEnabled(false);
        ui->removePrivilegeButton->setEnabled(false);
        ui->removeUserButton->setEnabled(false);
        */
        UserData *user = new UserData;
        user->setUserName(item->text());
        emit loadAccessList(user); // hm....??? need to pass itme->text()(a username) value
    }
    else
    {
        //Do nothing
    }
}

void ManagementWidget::on_addUserButton_clicked()
{
    //Create addUserDialog exec..
    AddUserDialog *addUserDialog = new AddUserDialog;
    connect(addUserDialog, SIGNAL(sendNewUserInfo(QString, QString,int)),
            this, SLOT(receiveNewUserInfoFromDialog(QString,QString,int)));
    qDebug() << "connecting addUserDialog and this";
    addUserDialog->setAttribute(Qt::WA_DeleteOnClose);
    addUserDialog->setModal(true);
    addUserDialog->exec();

}

void ManagementWidget::receiveNewUserInfoFromDialog(const QString &username, const QString &password,int privilege)
{
    // hash password here...
    qDebug() << "sending new user info to connection: " << username << " " << password;
    //ui->userList->addItem(username);
    //TEST
    //ui->userList->addItem("hello");
    //ui->userList->addItem("hello2");
    //ui->userList->addItem("hello3");
    //TESTEND

    QString str = password;
    QCryptographicHash hash(QCryptographicHash::Sha1);
    hash.addData(str.toUtf8());
    QString hashed_pw = hash.result().toHex();

    UserData *newUser = new UserData;
    newUser->setUserName(username);
    newUser->setHashedPassword(hashed_pw);
    newUser->setPrivilege(privilege);
    qDebug() << "new user: " + username + " passwodrd: " + password;
    emit addNewUser(newUser);
}

void ManagementWidget::addNewUserSuccess(const QString &username)
{
    qDebug() << "Trying to add " + username + " to userList";
    ui->userList->addItem(username);
}

void ManagementWidget::addNewUserFailed(const QString &errorMessage)
{
    QMessageBox::information(this, tr("Adding new user failed"), errorMessage);
}

void ManagementWidget::on_removeUserButton_clicked()
{
    ui->removeUserButton->setEnabled(false);
    ui->userList->setEnabled(false);
    qDebug() << "currently selected user is: " << ui->userList->currentItem()->text();
    if(ui->userList->currentItem()->text() != "")
    {
        UserData *user = new UserData;
        user->setUserName(ui->userList->currentItem()->text());
        emit removeUser(user);
    }
}

void ManagementWidget::removeUserSuccess()
{
    qDebug() << "trying to remove something..";
    delete ui->userList->currentItem();
    ui->removeUserButton->setEnabled(true);
    ui->userList->setEnabled(true);
}

void ManagementWidget::removeUserFailed(const QString &errorMessage)
{
    ui->removeUserButton->setEnabled(true);
    ui->userList->setEnabled(true);
    QMessageBox::information(this, tr("User removal failed"), errorMessage);
}

void ManagementWidget::receiveSelectedAccessRightFromDialog(const QString &selectedUser)
{
    if (selectedUser == ui->userList->currentItem()->text()) {
        addAccessRightFailed("Normal user has access to his/her own logs by default");
        return;
    }
    qDebug() << "userlist selected: " + ui->userList->currentItem()->text() + " received selcted user: " + selectedUser;
    newAccess = QString(selectedUser);
    emit addAccessRight(ui->userList->currentItem()->text(), selectedUser);
}

void ManagementWidget::on_addAccessRightButton_clicked()
{
    disableAllWidgets();

    //Create addUserDialog exec..

    AddPrivilegeDialog *addPrivilegeDialog = new AddPrivilegeDialog;
    //connect(this, SIGNAL())
    connect(addPrivilegeDialog, SIGNAL(sendSelectedUser(const QString &)),
            this, SLOT(receiveSelectedAccessRightFromDialog(const QString &)));
    connect(this, SIGNAL(sendAllUserListToPrivilegeAddDialog(QListWidget*)),
            addPrivilegeDialog, SLOT(receiveUserList(QListWidget *)));

    qDebug() << "emitting Send all userlist to privilege dialog";
    emit sendAllUserListToPrivilegeAddDialog(ui->userList);
    qDebug() << "connecting addPrivilegeDialog and management signal";
    addPrivilegeDialog->setAttribute(Qt::WA_DeleteOnClose);
    addPrivilegeDialog->setModal(true);
    addPrivilegeDialog->exec();
}

void ManagementWidget::on_removeAccessRightButton_clicked()
{
   /* if(ui->privilegeList->)
    {
        qDebug() << "nothing is selected on privilege list";
        QMessageBox::information(this, tr("Choose"),tr("Highlight one user"));
        return;
    }*/

    disableAllWidgets();
    emit removeAccessRight(ui->userList->currentItem()->text(), ui->privilegeList->currentItem()->text());

}

void ManagementWidget::addAccessRightSuccess()
{
    qDebug() << "Access Added, tring to add " + newAccess;
    if (newAccess != NULL || newAccess != "")
    {
        qDebug() << "sup";
        ui->privilegeList->addItem(newAccess);
        newAccess = "";
        enableAllWidets();
    }

    qDebug() << "new access added to database, but didn't receive the result back: this should never appear";

}

void ManagementWidget::addAccessRightFailed(const QString &errorMessage)
{
    enableAllWidets();
    QMessageBox::information(this, tr("Error in adding access right.."), errorMessage);
    //Error Messsage
}

void ManagementWidget::disableAllWidgets()
{
    ui->userList->setEnabled(false);
    ui->privilegeList->setEnabled(false);
    ui->addAccessRightButton->setEnabled(false);
    ui->addUserButton->setEnabled(false);
    ui->removeAccessRightButton->setEnabled(false);
    ui->removeUserButton->setEnabled(false);
}

void ManagementWidget::enableAllWidets()
{    ui->userList->setEnabled(true);
     ui->privilegeList->setEnabled(true);
     ui->addAccessRightButton->setEnabled(true);
     ui->addUserButton->setEnabled(true);
     ui->removeAccessRightButton->setEnabled(true);
     ui->removeUserButton->setEnabled(true);
}

void ManagementWidget::removeAccessRightSuccess()
{
    delete ui->privilegeList->currentItem();
    enableAllWidets();
}

void ManagementWidget::removeAccessRightFailed(const QString &errorMessage)
{
    QMessageBox::information(this, tr("removing access right failed"), errorMessage);
    enableAllWidets();
}

void ManagementWidget::on_privilegeList_itemClicked(QListWidgetItem *item)
{
    ui->removeAccessRightButton->setEnabled(true);
}
